define(function (require, exports, module) {

    exports.init = function (ctx, uuid, config) {
        var page = new $.PageDomFactory(uuid);
        require("apps/grid");
        var pageListComponent = new $.PageTreeListComponent(uuid, {
            view: {
                grid: "grid",
                colbar: "user_list_btn",
                myToolbar: "news_tb"
            },
            configs: {
                gridUrl: ctx + "/app/api/ebom/list?bomType=E_BOM_TYPE_UN_BUILD&tPBom.id=" + config.pBomId,
                idField: "id",
                treeField: "eName",
                gridColumns: [
                    {
                        field: 'id',
                        checkbox: true
                    },
                    {
                        field: 'temp1',
                        hidden: true
                    },
                    {
                        field: 'eName',
                        title: '名称',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: 'code',
                        title: '编码',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: 'pBomDes',
                        title: '设计结构',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: 'description',
                        title: '功能',
                        width: 100,
                        align: 'left',
                        hidden:true
                    },
                    {
                        field: 'remark',
                        title: '备注',
                        hidden: true,
                        width: 50,
                        align: 'left'
                    },
                    {
                        field: 'cover',
                        title: '封面',
                        width: 100,
                        align: 'left',
                        formatter: function (value, row, index) {
                            return "<img src=\"" + ctx + "/print/" + value + "\" style=\"width:48px;height:32px;\"/>"
                        }
                    },
                    {
                        field: 'createTime',
                        title: '创建时间',
                        width: 100,
                        align: 'left'
                    }
                ],
                onExpand:function(row){
                    if(row.children == undefined){
                        pageListComponent.grid.treegrid("loadChildren", {
                            url:ctx + "/app/api/ebom/children",
                            row:row
                        });
                    }
                }
            },
            events: {
                renderColBarBefore: function (key, row) {
                    return true;
                },
                onClickColBtn: function (context, btn, row) {
                    switch (btn.key) {
                        case "view":
                            whenView(context, row);
                            break;
                        default :
                            break;
                    }
                }
            }
        });

        var globalWindow = new $.PageWindow();


        function whenViewDetail(context, row) {
            //获取组成连接
            var pa = context.grid.treegrid("getParent", row.id);
            var parentId = pa.refId ? pa.refId : pa.id,
                curId = row.refId ? row.refId : row.id;
            globalWindow.create({
                width: 700,
                href: ctx + "/app/api/ebom/pBomDetailList?parentId=" + parentId + "&curId=" + curId,
                title: pa.eName + "(" + pa.code + ")" + "的组成结构"
            });
            globalWindow.open();
        }

        function whenView(context, row) {
            if(!row.bomGroup){
                context.detail("工艺装配结构详情查看", row);
            }else{
                whenViewDetail(context, row);
            }
        }
    };
});